home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / regexp.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  28KB  |  1,079 lines

  1. /* MODIFIED VERSION -- see below */
  2.  
  3. /* regcomp and regexec -- regsub and regerror are elsewhere
  4.  *
  5.  *    Copyright (c) 1986 by University of Toronto.
  6.  *    Written by Henry Spencer.  Not derived from licensed software.
  7.  *
  8.  *    Permission is granted to anyone to use this software for any
  9.  *    purpose on any computer system, and to redistribute it freely,
  10.  *    subject to the following restrictions:
  11.  *
  12.  *    1. The author is not responsible for the consequences of use of
  13.  *        this software, no matter how awful, even if they arise
  14.  *        from defects in it.
  15.  *
  16.  *    2. The origin of this software must not be misrepresented, either
  17.  *        by explicit claim or by omission.
  18.  *
  19.  *    3. Altered versions must be plainly marked as such, and must not
  20.  *        be misrepresented as being the original software.
  21.  *
  22.  * Beware that some of this code is subtly aware of the way operator
  23.  * precedence is structured in regular expressions.  Serious changes in
  24.  * regular-expression syntax might require a total rethink.
  25.  *
  26.  *    The third parameter to regexec was added by Martin C. Atkins.
  27.  *    Andy Tanenbaum also made some changes.
  28.  *      Eric Smith modified it a bit to compile with the MiNT library
  29.  */
  30.  
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <regexp.h>
  34. #include <stdio.h>
  35. #ifdef __TURBOC__
  36. # include <sys\types.h>
  37. #else
  38. # include <sys/types.h>
  39. #endif
  40. #ifndef _COMPILER_H
  41. #include <compiler.h>
  42. #endif
  43.  
  44. /* The first byte of the regexp internal "program" is actually this magic
  45.  * number; the start node begins in the second byte.
  46.  */
  47. #define    MAGIC    0234
  48.  
  49. /* The "internal use only" fields in regexp.h are present to pass info from
  50.  * compile to execute that permits the execute phase to run lots faster on
  51.  * simple cases.  They are:
  52.  *
  53.  * regstart    char that must begin a match; '\0' if none obvious
  54.  * reganch    is the match anchored (at beginning-of-line only)?
  55.  * regmust    string (pointer into program) that match must include, or NULL
  56.  * regmlen    length of regmust string
  57.  *
  58.  * Regstart and reganch permit very fast decisions on suitable starting points
  59.  * for a match, cutting down the work a lot.  Regmust permits fast rejection
  60.  * of lines that cannot possibly match.  The regmust tests are costly enough
  61.  * that regcomp() supplies a regmust only if the r.e. contains something
  62.  * potentially expensive (at present, the only such thing detected is * or +
  63.  * at the start of the r.e., which can involve a lot of backup).  Regmlen is
  64.  * supplied because the test in regexec() needs it and regcomp() is computing
  65.  * it anyway.
  66.  */
  67.  
  68. /* Structure for regexp "program".  This is essentially a linear encoding
  69.  * of a nondeterministic finite-state machine (aka syntax charts or
  70.  * "railroad normal form" in parsing technology).  Each node is an opcode
  71.  * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
  72.  * all nodes except BRANCH implement concatenation; a "next" pointer with
  73.  * a BRANCH on both ends of it is connecting two alternatives.  (Here we
  74.  * have one of the subtle syntax dependencies:  an individual BRANCH (as
  75.  * opposed to a collection of them) is never concatenated with anything
  76.  * because of operator precedence.)  The operand of some types of node is
  77.  * a literal string; for others, it is a node leading into a sub-FSM.  In
  78.  * particular, the operand of a BRANCH node is the first node of the branch.
  79.  * (NB this is *not* a tree structure:  the tail of the branch connects
  80.  * to the thing following the set of BRANCHes.)  The opcodes are:
  81.  */
  82.  
  83. /* Definition    number    opnd?    meaning */
  84. #define    END    0        /* no    End of program. */
  85. #define    BOL    1        /* no    Match "" at beginning of line. */
  86. #define    EOL    2        /* no    Match "" at end of line. */
  87. #define    ANY    3        /* no    Match any one character. */
  88. #define    ANYOF    4        /* str    Match any character in this string. */
  89. #define    ANYBUT    5        /* str    Match any character not in this
  90.              * string. */
  91. #define    BRANCH    6        /* node    Match this alternative, or the
  92.              * next... */
  93. #define    BACK    7        /* no    Match "", "next" ptr points backward. */
  94. #define    EXACTLY    8        /* str    Match this string. */
  95. #define    NOTHING    9        /* no    Match empty string. */
  96. #define    STAR    10        /* node    Match this (simple) thing 0 or more
  97.              * times. */
  98. #define    PLUS    11        /* node    Match this (simple) thing 1 or more
  99.              * times. */
  100. #define    OPEN    20        /* no    Mark this point in input as start of
  101.              * #n. */
  102.  /* OPEN+1 is number 1, etc. */
  103. #define    CLOSE    30        /* no    Analogous to OPEN. */
  104.  
  105. /* Opcode notes:
  106.  *
  107.  * BRANCH    The set of branches constituting a single choice are hooked
  108.  *        together with their "next" pointers, since precedence prevents
  109.  *        anything being concatenated to any individual branch.  The
  110.  *        "next" pointer of the last BRANCH in a choice points to the
  111.  *        thing following the whole choice.  This is also where the
  112.  *        final "next" pointer of each individual branch points; each
  113.  *        branch starts with the operand node of a BRANCH node.
  114.  *
  115.  * BACK        Normal "next" pointers all implicitly point forward; BACK
  116.  *        exists to make loop structures possible.
  117.  *
  118.  * STAR,PLUS    '?', and complex '*' and '+', are implemented as circular
  119.  *        BRANCH structures using BACK.  Simple cases (one character
  120.  *        per match) are implemented with STAR and PLUS for speed
  121.  *        and to minimize recursive plunges.
  122.  *
  123.  * OPEN,CLOSE    ...are numbered at compile time.
  124.  */
  125.  
  126. /* A node is one char of opcode followed by two chars of "next" pointer.
  127.  * "Next" pointers are stored as two 8-bit pieces, high order first.  The
  128.  * value is a positive offset from the opcode of the node containing it.
  129.  * An operand, if any, simply follows the node.  (Note that much of the
  130.  * code generation knows about this implicit relationship.)
  131.  *
  132.  * Using two bytes for the "next" pointer is vast overkill for most things,
  133.  * but allows patterns to get big without disasters.
  134.  */
  135. #define    OP(p)    (*(p))
  136. #define    NEXT(p)    (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
  137. #define    OPERAND(p)    ((p) + 3)
  138.  
  139. /* Utility definitions.
  140.  */
  141. #ifndef CHARBITS
  142. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  143. #else
  144. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  145. #endif
  146.  
  147. #define    CFAIL(m)    { regerror(m); return((char *)NULL); }
  148. #define    RFAIL(m)    { regerror(m); return((regexp *)NULL); }
  149. #define    ISMULT(c)    ((c) == '*' || (c) == '+' || (c) == '?')
  150. #define    META    "^$.[()|?+*\\"
  151.  
  152. /* Flags to be passed up and down.
  153.  */
  154. #define    HASWIDTH    01    /* Known never to match null string. */
  155. #define    SIMPLE        02    /* Simple enough to be STAR/PLUS operand. */
  156. #define    SPSTART        04    /* Starts with * or +. */
  157. #define    WORST        0    /* Worst case. */
  158.  
  159. /* Global work variables for regcomp().
  160.  */
  161. static char *regparse;        /* Input-scan pointer. */
  162. static int regnpar;        /* () count. */
  163. static char regdummy;
  164. static char *regcode;        /* Code-emit pointer; ®dummy = don't. */
  165. static long regsize;        /* Code size. */
  166.  
  167. /* Forward declarations for regcomp()'s friends.
  168.  */
  169. static char *reg __PROTO((int paren, int *flagp));
  170. static char *regbranch __PROTO((int *flagp));
  171. static char *regpiece __PROTO((int *flagp));
  172. static char *regatom __PROTO((int *flagp));
  173. static char *regnode __PROTO((int op));
  174. static void regc __PROTO((int b));
  175. static void reginsert __PROTO((int op, char *opnd));
  176. static void regtail __PROTO((char *p, char *val));
  177. static void regoptail __PROTO((char *p, char *val));
  178. static int regtry __PROTO((regexp *prog, char *string));
  179. static int regmatch __PROTO((char *prog));
  180. static size_t regrepeat __PROTO((char *p));
  181. static char *regnext __PROTO((char *p));
  182. #ifdef DEBUG
  183. static char *regprop __PROTO((char *op));
  184. #endif
  185.  
  186. /*
  187.  - regcomp - compile a regular expression into internal code
  188.  *
  189.  * We can't allocate space until we know how big the compiled form will be,
  190.  * but we can't compile it (and thus know how big it is) until we've got a
  191.  * place to put the code.  So we cheat:  we compile it twice, once with code
  192.  * generation turned off and size counting turned on, and once "for real".
  193.  * This also means that we don't allocate space until we are sure that the
  194.  * thing really will compile successfully, and we never have to move the
  195.  * code and thus invalidate pointers into it.  (Note that it has to be in
  196.  * one piece because free() must be able to free it all.)
  197.  *
  198.  * Beware that the optimization-preparation code in here knows about some
  199.  * of the structure of the compiled regexp.
  200.  */
  201. regexp *regcomp(e